package com.broadengate.dao.impl;


import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.broadengate.dao.AdminDao;
import com.broadengate.model.Atminfo;
import com.broadengate.model.Card;
import com.broadengate.model.Customer;
import com.broadengate.model.Deposit;
import com.broadengate.model.Transfer;
import com.broadengate.model.Withdraw;


/******
 * 提供管理员跟数据库的交互各个方法
 * 方法主要有login()实现登陆；listAllOperation()实现操作记录；DepositList()记录存款操作记录；
 * WithdrawList()记录取款操作记录；TrasferList()记录转账操作记录；findAtminfo()查找存款机的信息；
 * updateAtminfo()修改取款机信息；setPoundage()设置手续费费率；cancelAccount()销户；
 * openAccount()开户
 */
public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {
	@SuppressWarnings("unchecked")
	public boolean login(String adminName,String password) {
		System.out.println("AdminDaoImpl.login()进入");
		List<Atminfo> list = new ArrayList<Atminfo>();
		list= getHibernateTemplate().find("from Atminfo atminfo where atminfo.adminName='"+adminName+"' and atminfo.adminKey='"+password+"'");	
		return null!=list && list.size()==1 ? true : false;
	}
	
	@SuppressWarnings("unchecked")
	public List listAllOperation() {
		List<List> list=new ArrayList<List>();
		List<Deposit> listDeposit=new ArrayList();
		List<Withdraw> listWithdraw=new ArrayList();
		List<Transfer> listTransfer=new ArrayList();
		//Hibernate不需要通过使用getXXX（）来获取属性
		listDeposit=getHibernateTemplate().find("from Deposit deposit order by deposit.time asc");
		listWithdraw=getHibernateTemplate().find("from Withdraw withdraw order by withdraw.time asc");
		listTransfer=getHibernateTemplate().find("from Transfer transfer order by transfer.time asc");
		list.add(0,listDeposit);
		list.add(1,listWithdraw);
		list.add(2,listTransfer);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List DepositList() {		
		List<Deposit> listDeposit=new ArrayList();
		
		listDeposit=getHibernateTemplate().find("from Deposit deposit order by deposit.time asc");
		return listDeposit;
	}
	
	@SuppressWarnings("unchecked")
	public List WithdrawList() {	
		List<Withdraw> listWithdraw=new ArrayList();
		listWithdraw=getHibernateTemplate().find("from Withdraw withdraw order by withdraw.time asc");
		return listWithdraw;
	}
	
	@SuppressWarnings("unchecked")
	public List TrasferList() {
		List<Transfer> listTransfer=new ArrayList();
		listTransfer=getHibernateTemplate().find("from Transfer transfer order by transfer.time asc");		
		return listTransfer;
	}
	
	@SuppressWarnings("unchecked")
    public Atminfo findAtminfo(){
		List<Atminfo> list = new ArrayList<Atminfo>();
    	list= getHibernateTemplate().find("from Atminfo atminfo");		
		return null!=list && 0!=list.size() ? (Atminfo)list.get(0) : null;
    }

	public void updateAtminfo(Atminfo atminfo){
		getHibernateTemplate().update(atminfo);
	}

	@SuppressWarnings("unchecked")
	public void openAccount(Customer customer) {
		getHibernateTemplate().save(customer);
	}
	
	public void setPoundage(Atminfo atminfo){
		getHibernateTemplate().update(atminfo);
	}
	
	public void cancelAccount(String cardID){
		Session session=getSession();
		Transaction tx=session.beginTransaction();
		
		Card card=(Card) session.load(Card.class, Integer.parseInt(cardID));
		session.delete(card);
		tx.commit();	


	}
}
